﻿package  {
	import flash.display.MovieClip;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.display.BitmapData;
	import flash.printing.PrintJob;
	import flash.printing.PrintJobOptions;
	import flash.events.Event;

	public class Coloring extends MovieClip{
		private var bitmap:Bitmap;
		private var defaultBitmap:BitmapData;
		private var bitmapContainer:Sprite;
		private var itemsContainer:Sprite;
		private var colorPicker:ColorPicker;
		private var color:int = 0xFFFFFF;
		private var trashButton:TrashButton;
		private var printButton:PrintButton;
		private var nextButton:NextButton;
		private var brush:Bitmap;
		
		public function Coloring(bitmap:Bitmap) {
			bitmapContainer = new Sprite();
			bitmapContainer.graphics.beginFill(0xFFFFFF);
			bitmapContainer.graphics.drawRect(0, 0, 400, 400);
			bitmapContainer.addEventListener(MouseEvent.CLICK, fillWithColor);
			this.addChild(bitmapContainer);
			
			itemsContainer = new Sprite();
			itemsContainer.graphics.beginFill(0xEEEEEE);
			itemsContainer.graphics.drawRect(0, 0, 200, 400);
			itemsContainer.x = bitmapContainer.width;
			this.addChild(itemsContainer);
			
			colorPicker = new ColorPicker();
			itemsContainer.addChild(colorPicker);
			//setting different colors
			for (var i:int = 0; i < colorPicker.numChildren; i++) {
				if (colorPicker.getChildAt(i) is MovieClip) {
					var col:MovieClip = (MovieClip)(colorPicker.getChildAt(i));
					col.gotoAndStop(i);
					col.addEventListener(MouseEvent.CLICK, pickAColor);
				}
			}
			//creating brush image
			var data:BitmapData = new Brush(150, 150);
			brush = new Bitmap(data);
			itemsContainer.addChild(brush);
			brush.y = colorPicker.y + colorPicker.height + brush.height/4;
			//creating buttons
			trashButton = new TrashButton();
			itemsContainer.addChild(trashButton);
			trashButton.y = itemsContainer.height - trashButton.height;
			trashButton.x = 5;
			trashButton.addEventListener(MouseEvent.CLICK, buttonClicked);
			
			printButton = new PrintButton();
			itemsContainer.addChild(printButton);
			printButton.y = itemsContainer.height - printButton.height;
			printButton.x = trashButton.x + trashButton.width + 5;
			printButton.addEventListener(MouseEvent.CLICK, buttonClicked);
			
			nextButton = new NextButton();
			itemsContainer.addChild(nextButton);
			nextButton.y = itemsContainer.height - nextButton.height;
			nextButton.x = printButton.x + printButton.width + 5;
			nextButton.addEventListener(MouseEvent.CLICK, buttonClicked);
			
			this.bitmap = bitmap;
			this.bitmap.width = 400;
			this.bitmap.height = 400;
			bitmapContainer.addChild(this.bitmap);
			this.defaultBitmap = bitmap.bitmapData.clone();
		}
		
		private function fillWithColor(event:MouseEvent):void {
			if (this.bitmap.bitmapData.getPixel(event.localX, event.localY) > 0)
				this.bitmap.bitmapData.floodFill(event.localX, event.localY, color);	
		}
		
		private function pickAColor(event:MouseEvent):void {
			var mc = MovieClip(event.target);
			var data:BitmapData = new BitmapData(mc.width, mc.height);
			data.draw(mc);
			var oldColor:int = color;
			color = data.getPixel(event.target.width/2, event.target.height/2);
			updateBrush(oldColor);
		}
		
		private function updateBrush(oldColor:int):void {
			brush.bitmapData.lock();
			for (var i:int = 0; i<brush.width; i++)
				for(var j:int = 0; j<brush.height; j++) {
					if (brush.bitmapData.getPixel(i,j) == oldColor)
						brush.bitmapData.setPixel(i, j, color);
				}
			brush.bitmapData.unlock();
		}
		
		private function buttonClicked(event:MouseEvent):void {
			if (event.target is TrashButton)
				clearImage();
			else if (event.target is PrintButton)
				printImage();
			else if (event.target is NextButton)
				otherImage();
		}
		
		private function printImage():void {
			var myPrintJob:PrintJob = new PrintJob();
            var options:PrintJobOptions = new PrintJobOptions();
            options.printAsBitmap = true;
            
            myPrintJob.start();
            try {
                myPrintJob.addPage(bitmapContainer, null, options);
            }
            catch(e:Error) {}
            try {
            	myPrintJob.send();
            }
            catch (e:Error) {}
		}
		
		private function clearImage():void {
			bitmapContainer.removeChild(this.bitmap);
			bitmap.bitmapData = defaultBitmap.clone();
			bitmapContainer.addChild(this.bitmap);
		}
		
		private function otherImage():void {
			var popUp:PopUp = new PopUp();
			popUp.x = this.width/2;
			popUp.y = this.height/2;
			addChild(popUp);
			popUp.addEventListener("Close", onClosePopUp);
		}
		
		private function onClosePopUp(event:Event):void {
			event.target.addEventListener("Close", onClosePopUp);
			this.removeChild(MovieClip(event.target));
			if (PopUp(event.target).nextImage)
				close();

		}
		
		private function close():void {
			removeListeners();
			removeChildren();
			dispatchEvent(new Event("Close"));
		}
		
		private function removeListeners():void {
			trashButton.removeEventListener(MouseEvent.CLICK, buttonClicked);
			printButton.removeEventListener(MouseEvent.CLICK, buttonClicked);
			nextButton.removeEventListener(MouseEvent.CLICK, buttonClicked);
			bitmapContainer.removeEventListener(MouseEvent.CLICK, fillWithColor);
			for (var i:int = 0; i < colorPicker.numChildren; i++)
				if (colorPicker.getChildAt(i) is MovieClip)
					(MovieClip)(colorPicker.getChildAt(i)).removeEventListener(MouseEvent.CLICK, pickAColor);
		}
		
		private function removeChildren():void {
			var i:int;
			for (i=bitmapContainer.numChildren-1; i>=0; i--)
				bitmapContainer.removeChildAt(i);
			for (i=itemsContainer.numChildren-1; i>=0; i--)
				itemsContainer.removeChildAt(i);
			removeChild(itemsContainer);
			removeChild(bitmapContainer);
		}
	}
}